package Entity;

import Service.ContentContext;
import util.Config;

import java.io.*;
import java.util.*;

public class entityContext {
    private ContentContext contentContext;

    public void setContentContext(ContentContext contentContext) {
        this.contentContext = contentContext;
    }

    private Map<Integer, List<Question>> questions=new HashMap<>();
    private Map<Integer,User> mapUser=new HashMap<>();
    private Map<Integer,Question> paper=new HashMap<>();
    private Config config;
    public Map<Integer, Question> getPaper() {
        return paper;
    }

    public entityContext() {
        initUser();
        initQuestion();
    }
    public Map<Integer, List<Question>> getQuestions() {
        return questions;
    }
    //	private List/Set/Map
    // rules

    // 试卷解析
    public void initQuestion() {
        ArrayList<String> answerList = new ArrayList<>();
        ArrayList<Integer> scoreList = new ArrayList<>();
        ArrayList<Integer> levelList = new ArrayList<>();
        ArrayList<String> contentList = new ArrayList<>();
        ArrayList<String> themeList = new ArrayList<>();
        String path = User.class.getClassLoader().getResource("util/corejava.txt").getPath();
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String len;
        try {
            while ((len=bufferedReader.readLine())!=null){
                if (len.startsWith("@")){
                    String[] split = len.split(",");
                    answerList.add(split[0].split("=")[1]);
                    scoreList.add(Integer.valueOf(split[1].split("=")[1]));
                    levelList.add(Integer.valueOf(split[2].split("=")[1]));
                }
                else if(len.endsWith(":")||len.endsWith("。")){
                    themeList.add(len);
                }
                else {
                    contentList.add(len);
                }
            }
            for (int i = 0; i < scoreList.size(); i++) {
                Question question = new Question();
                ArrayList<Integer> ans = new ArrayList<>();
                ArrayList<String> opt = new ArrayList<>();
                question.setId(i+1);
                question.setTitle(themeList.get(i));
                question.setLevel(levelList.get(i));
                String s = answerList.get(i);
                String[] split = s.split("/");
                for (String s1:split) {
                    ans.add(Integer.valueOf(s1));
                }
                if (split.length>1){
                    question.setType(Question.MULTI_SELECTION);
                }
                else {
                    question.setType(Question.SINGLE_SELECTION);
                }
                question.setAnswers(ans);
                for (int j = i*4; j < i*4+4; j++) {
                    opt.add(contentList.get(j));
                }
                question.setOptions(opt);
                if (questions.get(question.getLevel())==null){
                    questions.put(question.getLevel(), new ArrayList<Question>());
                }
                questions.get(question.getLevel()).add(question);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 用户解析
    public void initUser() {
        String path = User.class.getClassLoader().getResource("util/user.txt").getPath();
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String len="";
        while (true){
            try {
                if (!(null != (len = bufferedReader.readLine()))) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            User user = parseUser(len);
            mapUser.put(user.getId(), user);
        }
    }

    private User parseUser(String len) {
        String[] split = len.split(":");
        User user = new User(Integer.parseInt(split[0]), split[1], split[2], split[3], split[4]);
        return user;
    }

    // 考试规则解析
    public String getRules() {
        String path = User.class.getClassLoader().getResource("util/rule.txt").getPath();
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String s="";
        String rule="";
        while (true){
            try {
                if (!(null != (s = bufferedReader.readLine()))) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            rule=rule+s+"\n";
        }
        return rule;
    }

    public User getUserid(int id) {
        User user = mapUser.get(id);
        return user;
    }

    public List<Question> findQuestions(int level) {
        return new ArrayList<Question>(questions.get(level));
    }


    // 其他文件内容的解析
}
